import com.alibaba.algo.Application;
import com.alibaba.algo.core.base.HttpComponnent;
import com.alibaba.algo.core.req.HttpRequestDTO;
import com.alibaba.algo.core.resp.HttpResponseDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: glorysongglory
 * Date: 2020/11/13
 * Time: 14:16
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class RestTest {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HttpComponnent httpComponnent;

    @Test
    public void testGet() {
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://127.0.0.1", String.class);
        System.out.println(responseEntity.getBody());
    }

    @Test
    public void testGetWithPara() {
        Map<String, Object> paraMap = new LinkedHashMap();
        paraMap.put("a", 1);
        paraMap.put("b", "http://abc.com");

        StringBuffer stringBuffer = new StringBuffer();
        String url = "";
        Iterator iterator = paraMap.entrySet().iterator();
        if (iterator.hasNext()) {
            stringBuffer.append("?");
            Object element;
            while (iterator.hasNext()) {
                element = iterator.next();
                Map.Entry<String, Object> entry = (Map.Entry) element;
                //过滤value为null，value为null时进行拼接字符串会变成 "null"字符串
                if (entry.getValue() != null) {
                    stringBuffer.append(element).append("&");
                }
                url = stringBuffer.substring(0, stringBuffer.length() - 1);
            }
        }

        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://127.0.0.1/t/get1" + url, String.class);
        System.out.println(responseEntity.getBody());
    }


    @Test
    public void testPostWithPara() {
//        HttpHeaders headers = new HttpHeaders();
//        headers.set("token","token111111");
////        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//        map.add("a", "1");
//        map.add("b", "http://abc.com");
//
//        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers);
//
//        String result = restTemplate.postForObject("http://127.0.0.1/t/post1", request, String.class);
//        System.out.println(result);

        HttpRequestDTO httpRequestDTO = new HttpRequestDTO();
        httpRequestDTO.setUrl("http://localhost/t/get1");
        httpRequestDTO.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpRequestDTO.setHeaderParaMap(new LinkedHashMap() {{
            put("a", "a1");
            put("b", "b1");
        }});
        httpRequestDTO.setParamMap(new LinkedHashMap(){{
            put("a",1);
            put("b",2);
            put("c",3);
        }});
//        HttpResponseDTO<String> responseDTO= httpComponnent.doPost(httpRequestDTO,String.class);
        HttpResponseDTO<String> responseDTO= httpComponnent.doGet(httpRequestDTO,String.class);
        System.out.println(responseDTO);

    }

}
